package cn.base.project.utils.http;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Linwq on 2017/12/21.
 */
public class BaseHttpUtils {
    /**
     * 判断请求的Accept是接受类型否是json
     * @param request
     * @return
     */
    public static Boolean ifJson(HttpServletRequest request){
        String hearder = request.getHeader("Accept");
        return hearder != null && hearder.contains("json");
    }

    /**
     * Controller 返回json
     * @param response
     * @param o
     * @return
     */
    public static Object noViewRTJson(HttpServletResponse response, Object o) throws IOException {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        MediaType jsonMimeType = MediaType.APPLICATION_JSON;
        jackson2HttpMessageConverter.write(o,jsonMimeType,new ServletServerHttpResponse(response));
        return null;
    }
}
